/**
 * Perform the element default action.
 *
 * @param {JSEvent} [event] the event that triggered the action
 *
 * @properties={typeid:24,uuid:"14E3E398-BDC7-487E-9D3E-C6F034C4AE23"}
 * @AllowToRunInFind
 */
function buscar(event) 
{
	if (globals.marca_primer_find == 0)
	{
		globals.marca_primer_find=1
		var estado_find = controller.find()
		if (estado_find == false)
		{
			globals.DIALOGS.showWarningDialog('Aviso','No hay movimientos de cuenta corriente','OK')
		   	return;
		}
		proveedor_id 	= globals.gProveedorDesde;
		saldo 			= '!= 0';
		
		var found = controller.search()
		if (found == 0)
		{
			globals.DIALOGS.showWarningDialog('Aviso','No hay movimientos de ctacte para ese proveedor','OK')
			var $win = application.getWindow('imput_ctacte');
			$win.hide()
			$win.destroy()	
		}
	}
}

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"80480F16-E5A4-44D2-A8C5-5962E8FBD495"}
 */
function inicio(firstShow, event) 
{
	globals.gtotal_creditos = 0;
	globals.gtotal_debitos = 0;
	globals.gtotal_comprob = 0;
	buscar();
	totales(); 
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} [event] the event that triggered the action
 *
 * @properties={typeid:24,uuid:"84A13F84-9FCC-4AB7-9589-1564AA82E94C"}
 */
function totales(event) 
{
	globals.gtotal_creditos = 0;
	globals.gtotal_debitos	= 0;
	globals.gtotal_comprob	= 0;
	var $total = databaseManager.getFoundSetCount(foundset)
	for (var i = 1 ; i <= $total ; i++)
	{
		var $rcd = foundset.getRecord(i)
		if ($rcd.marcado_ctacte == 1)
		{
			if($rcd.ctacte_pr_to_tab_mov_pr.d_h == 2)
				//globals.gtotal_debitos+=saldo_signo; // 21/12/2010
				globals.gtotal_debitos += $rcd.saldo;
			else
				//globals.gtotal_creditos+=saldo_signo;  // 21/12/2010
				globals.gtotal_creditos += $rcd.saldo;
			//globals.gtotal_comprob=globals.gtotal_debitos + globals.gtotal_creditos; // 21/12/2010
			globals.gtotal_comprob = globals.gtotal_debitos - globals.gtotal_creditos;
		}
	}
}

/**
 * Handle changed data.
 *
 * @param {Object} oldValue old value
 * @param {Object} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"5F3AE109-552A-4858-8B09-67141AD91FCF"}
 */
function onDataChange_marcado_ctacte(oldValue, newValue, event) 
{
	totales();    /// cuando hay muchos reg. hacer la pasada por de todos los reg por pantalla
	return true
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"E2E89475-2E93-4B98-A60F-4618230DB063"}
 */
function marcar_todo(event) 
{
	globals.gtotal_creditos = 0;
	globals.gtotal_debitos	= 0;
	globals.gtotal_comprob	= 0;
	var $total = databaseManager.getFoundSetCount(foundset)
	for (var i = 1 ; i <= $total ; i++)
	{
		var $rcd = foundset.getRecord(i)
		$rcd.marcado_ctacte = 1;
		if($rcd.ctacte_pr_to_tab_mov_pr.d_h == 2)
			//globals.gtotal_debitos+=saldo_signo;  // 21/12/2010
			globals.gtotal_debitos += $rcd.saldo;
		else
			// globals.gtotal_creditos+=saldo_signo;  // 21/12/2010
			globals.gtotal_creditos += $rcd.saldo;			
	}
	// globals.gtotal_comprob=globals.gtotal_debitos + globals.gtotal_creditos; //21/12/2010
	globals.gtotal_comprob = globals.gtotal_debitos - globals.gtotal_creditos;
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"A9F3CFCD-98F6-48E0-9302-F3211A3A38B2"}
 */
function desmarcar_todo(event) 
{	
	var $total = databaseManager.getFoundSetCount(foundset)
	for (var i = 1 ; i <= $total ; i++)
	{
		var $rcd = foundset.getRecord(i)
		if ($rcd.marcado_ctacte == 1)
			$rcd.marcado_ctacte = 0;
	}
	globals.gtotal_creditos = 0;
	globals.gtotal_debitos  = 0;
	globals.gtotal_comprob  = 0;
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"08CDC3C5-7C6D-4E56-8B92-8E4BAFF52042"}
 */
function confirmar(event) 
{
	//crear los reg. de imput detalle
	
	//borrar  lo que tenga en imput_detalle 
	forms.imput_pr_1.imput_cabecera_to_imput_detalle.deleteAllRecords()
	
	//recorre los marcados = 1
	var found = forms.imput_pr_3.controller.getMaxRecordIndex()	
	if (found > 0)
	{
		globals.gtotal_comprob = 0 
		var aux_saldo = 0
		for (var i = 1 ; i <= found; i++)
		{		 
			forms.imput_pr_3.controller.setSelectedIndex(i)
			if (forms.imput_pr_3.marcado_ctacte == 1)
			{
				//grabar lo que marcado en imput_detalle
				forms.imput_pr_1.imput_cabecera_to_imput_detalle.newRecord()
				  
				forms.imput_pr_1.imput_cabecera_to_imput_detalle.cod_mov		= cod_mov
				forms.imput_pr_1.imput_cabecera_to_imput_detalle.fecha			= fecha_mov
				forms.imput_pr_1.imput_cabecera_to_imput_detalle.importe		= globals.redondeo(saldo)
				forms.imput_pr_1.imput_cabecera_to_imput_detalle.imp_original	= globals.redondeo(saldo)
				forms.imput_pr_1.imput_cabecera_to_imput_detalle.nro_comprob	= nro_factura
				forms.imput_pr_1.imput_cabecera_to_imput_detalle.letra			= letra
				forms.imput_pr_1.imput_cabecera_to_imput_detalle.sucursal		= sucursal
				if (concepto == null)
					concepto = " ";
				forms.imput_pr_1.imput_cabecera_to_imput_detalle.concepto		= concepto
				forms.imput_pr_1.imput_cabecera_to_imput_detalle.ctacte_pr_id	= ctacte_pr_id
				if (ctacte_pr_to_tab_mov_pr.d_h == 1)
				{
				  	aux_saldo = globals.redondeo(saldo * (-1))
				}
				else
				{
				  	aux_saldo = globals.redondeo(saldo) 
				} 
				globals.gtotal_comprob += aux_saldo
			}
		}
	}
	var $win = application.getWindow('imput_ctacte');
	$win.hide()
	$win.destroy()	
}
